/*
 * Created on 2004/12/08
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.snack;


public class ESPS implements Cloneable{
    private boolean voicing;
    private double pitch;
    private double measurements;
    private double correlation;
    public String toString(){
        return ""+pitch+","+voicing+","+measurements+","+correlation;
    }
    public double getCorrelation() {
        return correlation;
    }
    public void setCorrelation(double correlation) {
        this.correlation = correlation;
    }
    public double getMeasurements() {
        return measurements;
    }
    public void setMeasurements(double measurements) {
        this.measurements = measurements;
    }
    public double getPitch() {
        return pitch;
    }
    public void setPitch(double pitch) {
        this.pitch = pitch;
    }
    public boolean isVoicing() {
        return voicing;
    }
    public void setVoicing(boolean voicing) {
        this.voicing = voicing;
    }
    
    public Object clone() {
    	 try {
             return (super.clone());
         } catch (CloneNotSupportedException e) {
             throw (new InternalError(e.getMessage()));
         }
    }
    
    public static int getNoVoiceAverageMeasurement(ESPS[] espses,boolean careMinVoice){
    	int ret=0;
    	if(espses!=null){
			long max=0;
			int index=0;
			double min=10000;
			for (int i = 0; i < espses.length; i++) {
				
				if(!espses[i].isVoicing()){
					max+=espses[i].getMeasurements();
					index++;
				}else{
					if(espses[i].getMeasurements()<min){
						min=espses[i].getMeasurements();
					}
				}
			}
			if(index>0){
				ret=(int) (max/index);
				if(careMinVoice && ret>min){
					ret=(int) min;
				}
			}
		}
    	return ret;
    }
    
    public static ESPSStatics analyzeESPS(ESPS[] espses){

    
    	 if(espses!=null){
 			long total=0;
 			int index=0;
 			double min=10000;
 			double max=0;
 			int average=0;
 			
 			long voicetotal=0;
 			int voiceindex=0;
 			double voicemin=10000;
 			double voicemax=0;
 			int voiceAverage=0;
 			for (int i = 0; i < espses.length; i++) {
 				
 				if(!espses[i].isVoicing()){
 					total+=espses[i].getMeasurements();
 					index++;
 					if(espses[i].getMeasurements()<min){
 						min=espses[i].getMeasurements();
 					}
 					if(espses[i].getMeasurements()>max){
 						max=espses[i].getMeasurements();
 					}
 				}else{
 					voicetotal+=espses[i].getMeasurements();
 					voiceindex++;
 					if(espses[i].getMeasurements()<voicemin){
 						voicemin=espses[i].getMeasurements();
 					}
 					if(espses[i].getMeasurements()>voicemax){
 						voicemax=espses[i].getMeasurements();
 					}
 				}
 			}
 			
 			if(index>0){
 				average=(int) (total/index);
 			}
 			
 			if(voiceindex>0){
 				voiceAverage=(int) (voicetotal/voiceindex);
 			}
 			return new ESPSStatics(index,(int)min,(int)max,average,voiceindex,(int)voicemin,(int)voicemax,voiceAverage);
 		}
    	 return null;
    }
    
    public static class ESPSStatics{
    	private int averageNoVoice;
    	private int averageVoice;
    	private int minVoice;
    	private int maxVoice;
    	private int minNoVoice;
    	private int maxNoVoice;
    	private int voiceIndex;
    	private int noVoiceIndex;
    	public ESPSStatics(int noVoiceIndex,int minNoVoice,int maxNoVoice,int averageNoVoice,int voiceIndex,int minVoice,int maxVoice,int averageVoice){
    		this.minNoVoice=minNoVoice;
    		this.maxNoVoice=maxNoVoice;
    		this.averageNoVoice=averageNoVoice;
    		this.minVoice=minVoice;
    		this.maxVoice=maxVoice;
    		this.averageVoice=averageVoice;
    		this.voiceIndex=voiceIndex;
    		this.noVoiceIndex=noVoiceIndex;
    	}

		public int getMinVoice() {
			return minVoice;
		}
		public void setMinVoice(int minVoice) {
			this.minVoice = minVoice;
		}
		public int getMaxVoice() {
			return maxVoice;
		}
		public void setMaxVoice(int maxVoice) {
			this.maxVoice = maxVoice;
		}
		public int getMinNoVoice() {
			return minNoVoice;
		}
		public void setMinNoVoice(int minNoVoice) {
			this.minNoVoice = minNoVoice;
		}
		public int getMaxNoVoice() {
			return maxNoVoice;
		}
		public void setMaxNoVoice(int maxNoVoice) {
			this.maxNoVoice = maxNoVoice;
		}

		public int getAverageNoVoice() {
			return averageNoVoice;
		}

		public void setAverageNoVoice(int averageNoVoice) {
			this.averageNoVoice = averageNoVoice;
		}

		public int getAverageVoice() {
			return averageVoice;
		}

		public void setAverageVoice(int averageVoice) {
			this.averageVoice = averageVoice;
		}

		public int getNoVoiceIndex() {
			return noVoiceIndex;
		}

		public void setNoVoiceIndex(int noVoiceIndex) {
			this.noVoiceIndex = noVoiceIndex;
		}

		public int getVoiceIndex() {
			return voiceIndex;
		}

		public void setVoiceIndex(int voiceIndex) {
			this.voiceIndex = voiceIndex;
		}
    }
}